from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import time
import pytest
@pytest.fixture(scope='session')
def driver():
    driver=webdriver.Edge()
    driver.get('https://www.jiuxian.com/')
    driver.maximize_window()
    yield driver
    driver.quit()
def test_jiuxian(driver):
    # 点击请登录
    driver.find_element(By.XPATH,'/html/body/div[2]/div/div[1]/div/a[1]').click()
    time.sleep(2)
    # 断言账号登录
    dy1=driver.find_element(By.XPATH,'//*[@id="loginform"]/div/div/div/div/div[1]/div[1]/a[1]').text
    print(dy1)
    time.sleep(2)
    # 输入用户名和密码
    driver.find_element(By.NAME,'uname').send_keys('15336710135')
    driver.find_element(By.NAME,'_pwd').send_keys('002816Hj')
    # 强制等待:强制让他等待10秒钟
    time.sleep(10)
    # 点击登录
    # driver.find_element(By.ID,'save').click()
    # 键盘事件
    driver.find_element(By.NAME, '_pwd').send_keys(Keys.ENTER)
    # # 鼠标事件
    # ActionChains(driver).drag_and_drop(driver.find_element(By.NAME,'key')).perform()
    # # 进入框架
    # driver.switch_to.frame(driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[1]/div[2]/a[2]'))
    # # 退出框架
    # driver.switch_to.default_content()
    # # 弹窗
    # con=driver.switch_to.alert
    # con.accept()
    # # 下拉框
    # pro=Select(driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[1]/div[2]/a[2]'))
    # pro.select_by_value('2')
    # 隐式等待：在3秒内找到元素执行下一步，没找到则等3秒回报错
    driver.implicitly_wait(3)
    # 点击确定
    driver.find_element(By.CSS_SELECTOR,'.bindSub').click()
    time.sleep(2)
    # 输入搜索框
    driver.find_element(By.NAME,'key').send_keys('茅台')
    time.sleep(2)
    # 点击搜索按钮
#     显示等待：总共20秒，每过0.5秒查找一次，找到执行下一步，没找到则在过0.5秒找一次，直到找到为止
    xs=WebDriverWait(driver,20,0.5).until(Ec.presence_of_element_located((By.XPATH,'//input[contains(@class,"search-btn")]')))
    xs.click()
#     断言茅台
    dy2=driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[1]/div[2]/a[2]').text
    assert "茅台" ==dy2
    print(dy2)
    # 点击商品
    driver.find_element(By.XPATH,'//img[@src="https://img06.jiuxian.com/2018/0731/41f8afccc3384c689f12948c02dce5e14.jpg"]').click()
    time.sleep(2)
    # 页面跳转
    driver.switch_to.window(driver.window_handles[-1])
    # 断言
    dy3=driver.find_element(By.XPATH,'//span[starts-with(@class,"d-cur")]').text
    print(dy3)
    # 点击 添加购物车 id="addToCartForDetail"，name="detail_jiarugouwuche"
    driver.find_element(By.XPATH,'//*[@id="addToCartForDetail"]').click()
    time.sleep(2)
    # 点击查看购物车 class="u-buy-go"，name="detail_chakangouwuche"
    driver.find_element(By.XPATH,'//*[@id="u-buy-layId"]/div/div[3]/a[2]').click()
    time.sleep(2)
    # 点击断言我的购物车
    dy4=driver.find_element(By.CLASS_NAME,'my-cart-tit').text
    print(dy4)
    # 点击去结算
    driver.find_element(By.XPATH,'//*[@id="cart_container"]/div[3]/div[3]/a[1]').click()
    time.sleep(2)
    # 断言确认订单信息
    dy5=driver.find_element(By.XPATH,'/html/body/div[2]/div/div[2]/div[2]/span[2]')
    print(dy5.text)
    print(dy5.tag_name)
    print(dy5.size)
    print(dy5.get_attribute(''))
    print(dy5.is_displayed())
    print(dy5.is_selected())

    time.sleep(2)
    # 断言金额
    dy6=driver.find_element(By.XPATH,'//*[@id="totalPriceInfo"]').text
    print(dy6)
    time.sleep(2)
    # 断言运费
    dy7=driver.find_element(By.XPATH,'//*[@id="freightPriceInfo"]').text
    print(dy7)
    time.sleep(2)
    # 断言总金额
    dy8=driver.find_element(By.XPATH,'/html/body/div[3]/div/div[7]/div/div[2]/div[1]/p[1]/em').text
    print(dy8)
    time.sleep(2)
    # 点击提交订单
    driver.find_element(By.XPATH,'//*[@id="submitOrder"]').click()
    time.sleep(2)
    # 断言
    dy9=driver.find_element(By.XPATH,'/html/body/div[4]/div/div[1]/ul/li[1]').text
    print(dy9)
    time.sleep(2)
    # 断言订单编号
    dy10=driver.find_element(By.XPATH,'/html/body/div[4]/div/div[1]/ul/li[3]/em').text
    print(dy10)